package com.itheima.pinda.web;

import com.alibaba.fastjson.JSONObject;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.LocationEntity;
import com.itheima.pinda.service.KafkaSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 提供http接口方式接收司机端上报的位置信息
 *
 * @author llhuang10
 * @date 2021/10/21 9:01
 */
@Slf4j
@Validated
@Api(tags = "车辆轨迹服务")
@RestController
@RequestMapping("/netty")
public class NettyController {

    /**
     * 将车辆定位信息发送到Kafka队列
     *
     * @param locationEntity 位置信息
     * @return result
     */
    @PostMapping("/push")
    @ApiOperation("将车辆定位信息发送到Kafka队列")
    public Result push(@Valid @RequestBody LocationEntity locationEntity) {
        String message = JSONObject.toJSONString(locationEntity);
        boolean res = KafkaSender.send(KafkaSender.MSG_TOPIC, message);
        log.info("HTTP接口方式推送位置信息到kafka: {}", message);
        return res ? Result.ok() : Result.error("发送消息失败!");
    }
}
